home *** CD-ROM | disk | FTP | other *** search
- /* Scroll.c
- * Handle window scrolling in Writeswell, Jr.
- * ©1992 Working Software, Inc.
- * This source code is copyrighted. Permission is granted to use the Word Services
- * portion of the Writeswell Jr. source code in your own programs, but you
- * may not distribute the Writeswell Jr. word-processor code as a
- * commercial product. If you modify the code, please do not call it
- * Writeswell Jr. (or Writeswell.) This will ensure that people understand the
- * program and don’t have to deal with a number of different versions with
- * who-knows-what going on in the code.
- *
- * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
- * 18 Apr 92 Mike Crawford
- */
-
- #include <EPPC.h>
- #include <AppleEvents.h>
- #include <AEObjects.h>
- #include "AERegistry.h"
- #include "TBConstants.h"
- #include "TBGlobals.h"
- #include "Scroll.h"
-
-
- void DoControl( WindowPtr theWindow, ControlHandle ctlHdl, short partCode, Point where )
- {
- short oldVal;
- short newVal;
-
- gScrollWindow = theWindow;
-
- switch ( partCode ){
- case inUpButton:
- case inDownButton:
- case inPageUp:
- case inPageDown:
-
- TrackControl( ctlHdl, where, TrackScrollBar );
- break;
- case inThumb:
- oldVal = GetCtlValue( ctlHdl );
- TrackControl( ctlHdl, where, (ProcPtr)NULL );
- newVal = GetCtlValue( ctlHdl );
-
- if ( newVal != oldVal ){
- ScrollText( oldVal, newVal, ctlHdl );
- }
-
- break;
- }
-
- return;
- }
-
- pascal void TrackScrollBar( ControlHandle ctlHdl, short partCode )
- {
- short max;
- short min;
- short val;
- short newVal;
-
- max = GetCtlMax( ctlHdl );
- min = GetCtlMin( ctlHdl );
- val = GetCtlValue( ctlHdl );
- newVal = val;
-
- switch ( partCode ){
- case inUpButton:
- if ( val > min )
- newVal = val - 1;
- break;
- case inDownButton:
- if ( val < max )
- newVal = val + 1;
- break;
- case inPageUp:
- newVal = val - gLinesPerPage;
-
- if ( newVal < min )
- newVal = min;
- break;
-
- case inPageDown:
- newVal = val + gLinesPerPage;
-
- if ( newVal > max )
- newVal = max;
- break;
- }
-
- if ( newVal != val ){
- ScrollText( val, newVal, ctlHdl );
- }
-
- return;
- }
-
- void ScrollText( short oldVal, short newVal, ControlHandle ctlHdl )
- {
- TEHandle textH;
- short dV;
-
- SetCtlValue( ctlHdl, newVal );
-
- textH = (TEHandle)GetWRefCon( gScrollWindow );
-
- dV = ( oldVal - newVal ) * (*textH)->lineHeight;
-
- TEScroll( 0, dV, textH );
-
- return;
- }
-
- Boolean TrackContentClick( void )
- {
- Point where;
- Rect viewRect;
- TEHandle textH;
- ControlHandle ctlHdl;
- GrafPtr curPort;
-
- /* Track the mouse while it is clicked in the window, and scroll the textEdit
- * field if necessary. We must always return true. We have to set the port
- * so that GetMouse will have the right window's local coordinates.
- */
-
- GetPort( &curPort );
- SetPort( gScrollWindow );
-
- GetMouse( &where );
-
- textH = (TEHandle)GetWRefCon( gScrollWindow );
-
- viewRect = (*textH)->viewRect;
-
- ctlHdl = ((WindowPeek)gScrollWindow)->controlList; /* We know there's 1 control */
-
- /* If the mouse is above or below the window, we fake a mouse click on the scroll bar */
- if ( where.v < viewRect.top ){
- TrackScrollBar( ctlHdl, inUpButton );
- } else if ( where.v > viewRect.bottom ){
- TrackScrollBar( ctlHdl, inDownButton );
- }
-
- SetPort( curPort );
-
- return true;
- }
-
- void SizeVertScroll()
- {
- MoveControl( gVertScroll, thePort->portRect.right - 15, -1 );
- SizeControl( gVertScroll, 16, thePort->portRect.bottom - thePort->portRect.top - 13 );
-
- return;
- }
-
- void SetVertScroll( WindowPtr theWindow, ControlHandle scrollHdl )
- {
- short totalHeight;
- short viewHeight;
- TEHandle hTE;
- Rect viewRect;
- short nLines;
- short tweenHeight;
- short i;
-
- /* The limits of the scroll bar range from 0 to the first line of the last
- * "windowfull" of text. That is, if there are 50 lines of text, and 10 lines
- * would fit in the window, then the limits are 0 through 40. If the window is
- * resized so that 20 lines of text would fit, then the limits are 0 and 30. This
- * way, when the thumb is all the way at the bottom, the last line of text is at
- * the bottom of the window.
- */
-
- hTE = (TEHandle)GetWRefCon( theWindow );
-
- nLines = (*hTE)->nLines;
-
- totalHeight = TEGetHeight( 0, nLines - 1, hTE );
-
- viewRect = (*hTE)->viewRect;
- viewHeight = viewRect.bottom - viewRect.top;
-
- /* STUB if the last line of text does not fall at or below the bottom of the window,
- * scrol the text down so it does, but not so much that the top line of text is
- * lower than the top of the window.
- */
-
- if ( totalHeight < viewHeight ){
-
- /* All of the text will fit in the window. Scroll the text so the first
- * line is at the top, and turn off the scroll bar.
- */
-
- /* STUB Scroll the text */
-
- HiliteControl( scrollHdl, 255 );
-
- return;
- }
-
- /* The text does not all fit. Find out how many lines from the bottom would fit */
-
- for ( i = nLines - 1; i > 0; i-- ){
- tweenHeight = TEGetHeight( nLines, i, hTE );
-
- if ( tweenHeight >= viewHeight )
- break;
- }
-
- /* i is now the line number of the first line that would be in the window if
- * we had scrolled all the way to the bottom
- */
-
- SetCtlMax( scrollHdl, i );
- HiliteControl( scrollHdl, 0 );
-
- /* Save the number of lines on a page for later use by scroll bar tracker */
-
- gLinesPerPage = nLines - i;
-
- return;
- }